﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.ServiceProcess;
using System.Threading;
using MdbPhoneBook;
using MdbPhoneBook.Dal;
using MdbPhoneBook.Entities;
using ServiceStack;

namespace WinServiceAppHost
{
    static class Program
    {
        private const string ListeningOn = "http://*:1337/"; 

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
        {
            var appHost = new MdbPhoneBookAppHost();

            //Allow you to debug your Windows Service while you're developing it. 
#if DEBUG
            Console.WriteLine("Running WinServiceAppHost in Console mode");
            try
            {
                appHost.Init();
                appHost.Start(ListeningOn);

                Console.WriteLine("Press <CTRL>+C to stop.");
                Thread.Sleep(Timeout.Infinite);
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR: {0}: {1}", ex.GetType().Name, ex.Message);
                throw;
            }
            finally
            {
                appHost.Stop();
            }

            Console.WriteLine("WinServiceAppHost has finished");

#else
                        //When in RELEASE mode it will run as a Windows Service with the code below

                        ServiceBase[] ServicesToRun;
                        ServicesToRun = new ServiceBase[] 
                        { 
                                new WinService(appHost, ListeningOn) 
                        };
                        ServiceBase.Run(ServicesToRun);


#endif

        }
    }

    //Define the Web Services AppHost
    public class MdbPhoneBookAppHost : AppHostHttpListenerBase
    {
        public MdbPhoneBookAppHost()
            : base("HttpListener Self-Host", typeof (AllEmployees).Assembly)
        {
            // This Subscription and Publishing in production will leave in different classes and different places.
            // Specific instance of message bus SHOULD NOT be shared.
            HandleMq q = new HandleMq();
            q.Subscribe();
        }

        public override void Configure(Funq.Container container)
        {
            
        }
    }

    
    [Route("/v2/ShowPhoneBook/employees")]
    [Route("/v1/ShowPhoneBook/employees")]
    public class AllEmployees : IReturn<List<Employee>> { }

    [Route("/v2/ShowPhoneBook/employees/{EmployeeName}")]
    public class FilteredEmployees : IReturn<List<Employee>>
    {
        public string EmployeeName { get; set; }
    }

    [Route("/v2/ShowPhoneBook/versions")]
    public class Info : IReturn<string>
    {
    }

    public class AllEmployeesService : Service
    {
        private readonly HandleMq m_LogPublisher =  new HandleMq();

        public List<Employee> Get(AllEmployees request)
        {
            LogStatistic();

            var newList = (new EmployeesReadDal()).GetEmployees();
            return newList; 
        }

        public List<Employee> Get(FilteredEmployees request)
        {
            LogStatistic(request.EmployeeName);

            var newList = (new EmployeesReadDal()).GetFilteredEmployees(request.EmployeeName);
            return newList; 
        }

        public string Get(Info request)
        {
            string releaseNotes = 
                        @"v1 release notes:<br>Windows Service created and installed,<br>Redis basic update added (in sync mode)" + 
                        "<br><br>" + 
                        @"v2 release notes:<br>RabbitMq added,<br>Redis update moved to be done by subscription to Rabbit's Queue";
            return releaseNotes;
        }

        private void LogStatistic(string message = "")
        {
            try
            {
                // This Subscription and Publishing in production will leave in different classes and different places.
                // Specific instance of message bus SHOULD NOT be shared.
                m_LogPublisher.Publish(message);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
        }
    }

   
}